var productObj = [];

$("#addproducts").click(function (e) {
    productObj = []
    getProducts() 
}); 

function getProducts() {
    var products = $("#products").val();
    if (products.trim() == "") {
        alert("内容为空");
        return [];
    } else {
        var productarr = products.split(/[\n]/);
        productarr = trimSpace(productarr);
        out(productarr[0]);
        out(productarr[1]);
        out();
        for (let index = 0; index < productarr.length; index++) {
            productObj.push(strTojson(productarr[index]))
        }

        out(productObj);
        postProObj(productObj);
    }
}


    function postProObj(obj){
        $("#productsobj").val(JSON.stringify(obj)); 
    }


function strTojson(str) {
    var strarr = [];
    strarr = str.split(/\|/);
    productPathStr = strarr[0];
    productTitle = strarr[1];
    productTuStr = strarr[2];
    productText = strarr[3];

    var jsonObj = {};
    var productPatharr = productPathStr.split(/\//);
  //  productPatharr.splice(productPatharr.length - 1, 1);
    jsonObj['path'] = productPatharr;
    jsonObj['title'] = productTitle;
    jsonObj['tu'] = productTuStr.split(/\//);
    jsonObj['text'] = productText;

    return jsonObj;
}



function trimSpace(array) {
    for (var i = 0; i < array.length; i++) {

        if (array[i].trim() == "" || array[i] == null || typeof (array[i]) == "undefined") {
            array.splice(i, 1);
            i = i - 1;

        }
    }
    return array;
}


function out(text) {
    $("#productsshow").html($("#productsshow").html() + "&#10;" + text);
    console.log("js输出");
    console.log(text);
    console.log("js输出解锁");
}

